package com.voice.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.voice.core.entity.HttpHeader;
import com.voice.core.ResponseCodeEnum;
import com.voice.core.connection.entity.StreamData;

public class ResponseUtil {

    public static HttpHeader successHeader(){
        return errorHeader(ResponseCodeEnum.Success);
    }

    public static HttpHeader errorHeader(ResponseCodeEnum responseCodeEnum){
        HttpHeader header = new HttpHeader();
        header.code(responseCodeEnum.getCode());
        header.message(responseCodeEnum.getMessage());
        header.put("x-message-id", MessageUtil.messageId());
        header.put("x-generate-time",System.currentTimeMillis());
        return header;
    }

    public static StreamData success() {
        return success(null);
    }

    public static StreamData success(Object data) {
        StreamData streamData=new StreamData();
        streamData.addHeaders(successHeader());
        streamData.addData(JSONObject.toJSONString(data).getBytes());
        return streamData;
    }

    public static StreamData success(String topic,Object data) {
        return success(topic,0,data);
    }

    public static StreamData success(String topic,int qos,Object data) {
        StreamData streamData=new StreamData();
        streamData.addHeaders(successHeader());
        streamData.getHeaders().put("x-topic",topic);
        streamData.getHeaders().put("x-qos",qos);
        streamData.addData(JSONObject.toJSONString(data).getBytes());
        return streamData;
    }

    public static StreamData error(ResponseCodeEnum responseCodeEnum) {
        StreamData streamData=new StreamData();
        streamData.addHeaders(errorHeader(responseCodeEnum));
        return streamData;
    }

}
